package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: ManageController
 * Package: com.atguigu.gmall.product.controller
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-16 10:16
 * @Version 1.0
 */


@Api(tags = "后台管理控制器")
@RestController //@Controller{控制器}+@ResponseBody{返回数据变为Json}
@RequestMapping("/admin/product")

public class ManageController {

    @Autowired
    private ManageService manageService;


    /**
     * 查询一级分类所有数据
     *
     * @return
     */
    @ApiOperation(value = "获取一级分类数据")
    @GetMapping("/getCategory1")
    public Result getCategory1() {
        //  调用服务层方法获取数据.
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    /**
     * 查询二级分类所有数据
     *
     * @param category1Id
     * @return
     */
    @ApiOperation(value = "获取二级分类数据")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    /**
     * 获取三级分类数据
     *
     * @param category2Id
     * @return
     */
    @ApiOperation(value = "获取三级分类数据")
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    /**
     * 根据分类id获取平台属性集合
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @ApiOperation(value = "根据分类id获取平台属性集合")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable Long category1Id,
                                  @PathVariable Long category2Id,
                                  @PathVariable Long category3Id) {
        // 调用服务层方法
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    /**
     * 保存平台属性
     * 传递的参数Json---->JavaObject
     * @param baseAttrInfo：将前端传递的数据变为实体类
     * @return
     */
    @ApiOperation(value = "保存平台属性")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        // 调用服务层方法
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 回显平台属性值
     * @param attrId
     * @return
     */
    @ApiOperation(value = "根据平台属性id获取平台属性值集合")
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        // 调用服务层方法
        // 根据平台属性id获取平台属性值，有一定的风险，属性不存在，应然能获取到属性值
     //List<BaseAttrValue> baseAttrValueList = managerService.getAttrValueList(attrId);
     // 根据平台属性id 先查询平台属性，如果有平台属性，在获取平台属性值！
       BaseAttrInfo baseAttrInfo = this.manageService.getAttrInfo(attrId);

     return Result.ok(baseAttrInfo.getAttrValueList());
    }


}
